from lxml import etree
import requests


def Response(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36"
    }
    response = requests.get(url=url, headers=headers)
    return response


def GetCityData(response):
    html = response.text
    tree = etree.HTML(html)
    citys = tree.xpath('//ul[@class="city_table"]/li/a/text()')
    urls = tree.xpath('//ul[@class="city_table"]/li/a/@href')
    list_tuple = zip(citys, urls)
    return list_tuple


def GetRegionData(url, response):
    html = response.text
    tree = etree.HTML(html)
    regions = tree.xpath('//ul[@class="box"]/li[3]/a/text()')[1:]
    urls = tree.xpath('//ul[@class="box"]/li[3]/a/@href')[1:]
    urls = [url + i.split("/", 1)[1] for i in urls]
    list_tuple = zip(regions, urls)
    # print(list(list_tuple))
    return list_tuple


def GetPageData(url, response):
    html = response.text
    tree = etree.HTML(html)
    titles = tree.xpath('//ul[@class="content_list_wrap"]/li/a/text()')
    urls = tree.xpath('//ul[@class="content_list_wrap"]/li/a/@href')
    urls = [url + i for i in urls]
    visits = tree.xpath('//div[@class="left visited"]/span/@title')
    visits = [int(i) for i in visits]
    dates = tree.xpath('//div[@class="left date"]/@title')
    list_tuple = zip(titles, urls, visits, dates)
    return list_tuple


def GetJanZhi(response):
    html = response.text
    tree = etree.HTML(html)
    job_type = tree.xpath('//a[@class="job_type"]/text()')

    people = tree.xpath('//ul[@class="job_list"][1]/li[1]/span[2]/text()')
    people = [int(i) for i in people]
    job_address = tree.xpath('//ul[@class="job_list"][1]/li[2]/span[2]/text()')

    time_ask = tree.xpath('//ul[@class="job_list"][2]/li[1]/span[2]/text()')
    time_ask = [i.strip() for i in time_ask]
    job_kind = tree.xpath('//ul[@class="job_list"][2]/li[2]/span[2]/text()')
    working_time = tree.xpath('//ul[@class="job_list"][2]/li[3]/span[2]/text()')

    settlement = tree.xpath('//ul[@class="job_list"][3]/li[1]/span[2]/text()')
    settlement = [i.strip() for i in settlement]
    wagess = tree.xpath('//ul[@class="job_list"][3]/li[2]/span[2]/text()')
    # wagess = [i.strip() for i in wagess]
    wages = []
    for wage in wagess:
        wage = wage.strip()
        if "万元/小时" in wage:
            wage = int(wage.strip("万元/小时"))
            wage = wages.append(wage * 10000 * 8)
        elif "千元/小时" in wage:
            wage = int(wage.strip("千元/小时"))
            wage = wages.append(wage * 1000 * 8)
        elif "元/小时" in wage:
            wage = int(wage.strip("元/小时"))
            wage = wages.append(wage * 8)
        elif "万元/天" in wage:
            wage = int(wage.strip("万元/天"))
            wage = wages.append(wage * 10000)
        elif "千元/天" in wage:
            wage = int(wage.strip("千元/天"))
            wage = wages.append(wage * 1000)
        elif "元/天" in wage:
            wage = int(wage.strip("元/天"))
            wage = wages.append(wage)
        elif "万元/月" in wage:
            wage = int(wage.strip("万元/月"))
            wage = wages.append(int((wage * 10000) / 30))
        elif "千元/月" in wage:
            wage = int(wage.strip("千元/月"))
            wage = wages.append(int((wage * 1000) / 30))
        elif "元/月" in wage:
            wage = int(wage.strip("元/月"))
            wage = wages.append(int(wage / 30))
        elif "万元/年" in wage:
            wage = int(wage.strip("万元/年"))
            wage = wages.append(int((wage * 10000) / 365))
        elif "千元/年" in wage:
            wage = int(wage.strip("千元/年"))
            wage = wages.append(int((wage * 1000) / 365))
        elif "元/年" in wage:
            wage = int(wage.strip("元/年"))
            wage = wages.append(wage / 365)
        else:
            wage = wages.append("-1")
    job_detailed = tree.xpath('//div[@id="job_detail"]/text()')

    company = tree.xpath('//div[@class="company_info"]/a/text()')
    company_introduction = tree.xpath('//div[@class="company_info"]/p[1]/text()')
    company_address = tree.xpath('//div[@class="company_info"]/p[2]/text()')

    # print(job_type)
    # print(people)
    # print(job_address)
    # print(time_ask)
    # print(job_kind)
    # print(working_time)
    # print(settlement)
    # print(wages)
    # print(job_detailed)
    # print(company)
    # print(company_introduction)
    # print(company_address)
    # print("-----------------------------------------------------------------")
    list_tuple = zip(job_type, people, job_address, time_ask, job_kind, working_time, settlement, wages, job_detailed,
                     company, company_introduction, company_address)
    return list_tuple



